import os
dir = [x for x in os.listdir() if os.path.isdir(x)]
for i in dir:
    os.chdir(i)
    cases = [x for x in os.listdir() if x[-3:] == '.js']
    exitcodes = [x for x in os.listdir() if x[-4:] == 'code']
    failed = []

    for x in exitcodes:
        with open(x, 'r') as f:
            code = f.read(1)
            if code != '0':
                failed.append(x[:-9])
    os.chdir('..')

    lines = []
    lines.append(f'# {i}\n\n')
    lines.append(f'## Test cases\n\n')
    for case in cases:
        lines.append(f'{case}\n\n')

    lines.append(f'\n## Not pass cases\n\n')
    for case in failed:
        lines.append(f'{case}\n\n')

    lines.append(f'\n## Details\n\n')
    for case in cases:
        try:
            lines.append(f'### {case}\n\n')
            with open(f'{i}/{case}.exitcode', 'r') as f:
                code = f.readline()
            lines.append(f'Exit code: {code}\n\n')

            with open(f'{i}/{case}.out', 'r', encoding='utf-8') as f:
                if f.read() == '':
                    pass
                else:
                    lines.append('stdout:\n\n')
                    lines.append(f'```text\n')
                    f.seek(0)
                    lines.extend(f.readlines())
                    lines.append(f'\n```\n\n')

            with open(f'{i}/{case}.err', 'r', encoding='utf-8') as f:
                if f.read() == '':
                    pass
                else:
                    lines.append('stderr:\n\n')
                    lines.append(f'```text\n')
                    f.seek(0)
                    lines.extend(f.readlines())
                    lines.append(f'\n```\n\n')

        except FileNotFoundError:
            pass

    with open(f'{i}.md', 'w', encoding='utf-8') as f:
        f.writelines(lines)
